package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.bean.LogProjectItemBean;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.utils.soundUtil.UPlayer;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/1.
 */
public class LogFillAdapter extends BaseAdapter {
    Context context;
    List<LogProjectItemBean> listBeans;
    LayoutInflater inflater;

    boolean isedit = false;

    public LogFillAdapter(Context context, List<LogProjectItemBean> listBeans) {
        this.context = context;
        this.listBeans = listBeans;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return listBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LogProjectItemBean bean = (LogProjectItemBean) getItem(i);
        ItemHolldler itemHolldler = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_logfill, null);
            itemHolldler = new ItemHolldler(view);
            view.setTag(itemHolldler);
        } else {
            itemHolldler = (ItemHolldler) view.getTag();
        }

        try {
            itemHolldler.voice_remark.setOnClickListener(null);
            if (bean.getNum() == 0) {
                if (bean.getStage_name() != null || bean.getProject_statis() != null) {
                    open_item(itemHolldler);
                    itemHolldler.time.setText(bean.create_time);
                    itemHolldler.user.setText(bean.getUser());
                    itemHolldler.name.setText(SetText(bean.getStage_name()));
                    itemHolldler.value.setText(SetText(bean.getProject_statis()));
                } else {
                    nodata_item(itemHolldler);
                    //无项目
                    itemHolldler.time.setText(bean.create_time);
                    itemHolldler.user.setText(bean.getUser());
                    itemHolldler.remark.setText(bean.getCharacters_remark());
                    if (bean.getVoice_remark().length() > 0) {
                        itemHolldler.voice_remark.setVisibility(View.VISIBLE);
                        play(itemHolldler, bean.getVoice_remark() );
                    } else {
                        itemHolldler.voice_remark.setVisibility(View.GONE);
                    }
                }
            } else {
                if (bean.getStage_name() != null || bean.getProject_statis() != null) {
                    hide_item(itemHolldler);
                    itemHolldler.name.setText(SetText(bean.getStage_name()));
                    itemHolldler.value.setText(SetText(bean.getProject_statis()));
                } else {
                    //有项目
                    hide_buttom_item(itemHolldler);
                    itemHolldler.remark.setText(bean.getCharacters_remark());
                    if (bean.getVoice_remark().length() > 0) {
                        itemHolldler.voice_remark.setVisibility(View.VISIBLE);
                        play(itemHolldler, bean.getVoice_remark() );
                    } else {
                        itemHolldler.voice_remark.setVisibility(View.GONE);
                    }

                }
            }


        } catch (Exception e) {
        }
        return view;
    }


    private void play(ItemHolldler h, String url ) {
        double length=0;
        if (url != null && url.length() > 0) {
            if (UPlayer.getIntence(url).mPlayer!=null){
                length = ((double) UPlayer.getIntence(url).getDuration() / (double) 1000);
            }
        }

        if (length!=-1){
            SetTypeface.set(h.voice_remark, IconSourse.PLAY);
            h.voice_remark.append(" " + length);
        }
        h.voice_remark.setOnClickListener(new MClickListener(0, h, url) {
            @Override
            public void onClick(View view, int position, Object msg) {
                if (msg2 != null && msg2.toString().length() != 0) {
                    UPlayer.getIntence(msg2.toString()).start();
                } else {
                    ToastShow.showShort(context, "没有路径！");
                }
            }
        });
    }

    private String SetText(String s) {
        if (s == null || s.length() == 0) {
            return "暂无数据";
        }
        return s;
    }

    private void open_item(ItemHolldler h) {
        h.time.setVisibility(View.VISIBLE);
        h.line.setVisibility(View.VISIBLE);
        h.user.setVisibility(View.VISIBLE);
        h.item.setVisibility(View.VISIBLE);
        h.remark.setVisibility(View.GONE);
        h.no.setVisibility(View.GONE);
        h.voice_remark.setVisibility(View.GONE);
    }

    private void hide_item(ItemHolldler h) {
        h.time.setVisibility(View.GONE);
        h.line.setVisibility(View.GONE);
        h.user.setVisibility(View.GONE);
        h.item.setVisibility(View.VISIBLE);
        h.no.setVisibility(View.GONE);
        h.remark.setVisibility(View.GONE);
        h.voice_remark.setVisibility(View.GONE);
    }

    private void nodata_item(ItemHolldler h) {
        h.time.setVisibility(View.VISIBLE);
        h.line.setVisibility(View.VISIBLE);
        h.user.setVisibility(View.VISIBLE);
        h.item.setVisibility(View.GONE);
        h.no.setVisibility(View.VISIBLE);
        h.remark.setVisibility(View.VISIBLE);
        h.voice_remark.setVisibility(View.VISIBLE);
    }

    private void hide_buttom_item(ItemHolldler h) {
        h.time.setVisibility(View.GONE);
        h.line.setVisibility(View.GONE);
        h.user.setVisibility(View.GONE);
        h.item.setVisibility(View.GONE);
        h.no.setVisibility(View.GONE);
        h.remark.setVisibility(View.VISIBLE);
        h.voice_remark.setVisibility(View.VISIBLE);
    }


    class ItemHolldler {
        TextView time, user, name, remark, voice_remark, no;
        View line;
        EditText value;
        LinearLayout item;

        ItemHolldler(View v) {
            item = (LinearLayout) v.findViewById(R.id.log_item);
            time = (TextView) v.findViewById(R.id.log_create_time);
            line = v.findViewById(R.id.log_line);
            user = (TextView) v.findViewById(R.id.log_user);
            name = (TextView) v.findViewById(R.id.log_name);
            value = (EditText) v.findViewById(R.id.log_value);
            no = (TextView) v.findViewById(R.id.log_no);
            remark = (TextView) v.findViewById(R.id.log_remark);
            voice_remark = (TextView) v.findViewById(R.id.log_voice_remark);
            SetTypeface.set(voice_remark, IconSourse.PLAY);
            voice_remark.append(" 播放");
            if (!isedit) {
                value.setEnabled(false);
            }

        }
    }


}
